<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/common/css.jsp"%>
<%
	String path = request.getContextPath();
	request.setAttribute("ctx",path);
 %>
<HTML>
 <HEAD>
  <TITLE> ZTREE DEMO </TITLE>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <!-- 
  <link rel="stylesheet" type="text/css" href="${base }/css/caas_style.css" />
   -->
  <link rel="stylesheet" href="${base }/css/zTreeStyle/zTreeStyle.css" type="text/css">
  
  <script type="text/javascript" src="${base }/js/jquery/jquery-1.7.2.min.js"></script>
  <!-- 
  	<script type="text/javascript" src="${ctx }/js/jquery/jqueryextend.js"></script>  
   -->
  <script type="text/javascript" src="${base }/js/jquery/jquery.ztree-2.6.min.js"></script>
  <SCRIPT LANGUAGE="JavaScript">
  	
	
	var zTree1;
	var setting;
	setting = {
		checkable: false,
		async: true,
		asyncUrl: setAsyncUrl,  //获取节点数据的URL地址
		asyncParam: ["orgCode", "id"],  //获取节点数据时，必须的数据名称，例如：id、name
		asyncParamOther: ["test","true"], //其它参数 ( key, value 键值对格式)
		rootPID : -1,
		callback:{
			//beforeAsync: zTreeBeforeAsync
			//asyncSuccess: zTreeOnAsyncSuccess
			//asyncError: zTreeOnAsyncError
			click: zTreeOnClick				
		}
	};
	var zNodes =[];
 
	$(document).ready(function(){
		refreshTree();
	});
 
	function getTime() {
		var now= new Date();
		var hour=now.getHours();
		var minute=now.getMinutes();
		var second=now.getSeconds();
		return (hour+":"+minute+":"+second);	
	}
	
	function zTreeOnClick(event, treeId, treeNode){
		parent.showUser(treeNode.id);
	}
	
 	var tmpUrl = "";
	function setAsyncUrl(treeNode) {
		var orgCode = "";
		if(treeNode != null){
			orgCode = treeNode.id; 
		}
		tmpUrl = "${ctx}/org/findChildOrg?orgCode=" + orgCode;
		$("#getUrl").html(tmpUrl);
		return tmpUrl;
	}
	
	function dataFilter(treeId, parentNode, childNodes) {
		if (childNodes) {
			for(var i =0; i<childNodes.length; i++) {
				childNodes[i].name += "_filter";
			}
		}
		return childNodes;
	}
 
	function zTreeBeforeAsync(treeId, treeNode) {
		alert(treeNode['id']);
		if(treeNode == null){
			return false;
		}
		return true;
		//var r = $("#beforeAsyncTrue").attr("checked");
		//if (!r) alert("beforeAsync return false!!");
		//return r;
	}
	
	function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
		//alert(zTree1);
		//var node = zTree1.getNodeByTId(treeId + "_1");
		
		//alert(node.id);
		//if(node.id == 0)
			//zTree1.reAsyncChildNodes(null,"refresh");
		//var treeNode = {'id':'0','name':'用户树','icon':'','isParent':'true'};
		//zTree1.expandNode(treeNode,true,false,true);
		//zTree1.reAsyncChildNodes(treeNode,"refresh");
		//if (treeNode) {
		//	$("#onAsyncSuccessNode").html( "[" + getTime() + "]  treeId=" + treeId + ";<br/>&nbsp;tId=" + treeNode.tId + "; Name=" + treeNode.name );
		//} else {
		//	$("#onAsyncSuccessNode").html( "[" + getTime() + "]  treeNode is Root!");
		//}
	}
 
	function zTreeOnAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
		if (treeNode) {
			$("#onAsyncErrorNode").html( "[" + getTime() + "]  treeId=" + treeId + ";<br/>&nbsp;tId=" + treeNode.tId + "; Name=" + treeNode.name );
		} else {
			$("#onAsyncErrorNode").html( "[" + getTime() + "]  treeNode is Root!");
		}
	}
 
	function refreshTree(asyncUrl) {
		$("#getUrl").html("");
		$("#onAsyncSuccessNode").html("<br/>");
		$("#onAsyncErrorNode").html("<br/>");
 		
		if ((typeof asyncUrl) == "function") {
			$("#asyncUrlCode").html("setAsyncUrl");
		} else {
			$("#asyncUrlCode").html("\"" + asyncUrl + "\"" );
		}
 
		if ($("#filterN").attr("checked")) {
			setting.asyncDataFilter = null;
			$("#asyncDataFilterCode").html("null");
		} else if ($("#filterY").attr("checked")) {
			setting.asyncDataFilter = dataFilter;
			$("#asyncDataFilterCode").html("dataFilter");
		}
 
		zTree1 = $("#treeDemo").zTree(setting, zNodes);
		//var node = zTree1.getNodeByTId(treeId + "_1");
		//zTree1.reAsyncChildNodes(null,"refresh");
		zTree1.expandAll(true);
		//var node = zTree1.getNodeByTId(0);
		//alert(node);
		//var treeNode = {id:0,name:'用户树',icon:'',isParent:'true'};
		//zTree1.expandNode(treeNode,true,false,true);
		//zTree1.reAsyncChildNodes(treeNode,"refresh");
	}
 
	function reloadAsync() {
		var treeNode = zTree1.getSelectedNode();
		if (!treeNode) {
			alert("请先选中一个节点");
			return;
		}
		zTree1.reAsyncChildNodes(treeNode, $("#refreshType").attr("checked") ? "refresh" : "add")
	}
	
	function getNow() {
		var d = new Date();
		return d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "." + d.getMilliseconds()
	}
	
	//获得选中的用户ID
	function getSelectedNodeId(){
		var node = zTree1.getSelectedNode();
		//alert(node.orgCode + "," + node.orgId);
		var param = {};
		if(node != null){
			param.orgId = node.orgId;
			param.orgCode = node.orgCode;
		}else{
			param.orgId = -1;
			param.orgCode = "";
		}
		return param;
	}
	
	//添加用户
	function addNode(nodeId,nodeName,pNodeId){
		var newNode = "[{'id':" + nodeId + ",'name':" + nodeName + ",'icon', '','isParent', 'true'}]";
		var pNode = zTree1.getNodeByTId(pNodeId);
		zTree1.addNodes(pNode, newNode);
	} 
  </SCRIPT>
 </HEAD>
 
<BODY style="background-color: #E5F3FC;">
<TABLE border=0 width="150" class="tb1">
	<TR>
		<TD width=150px align=center valign=top>
		<div>
			<ul id="treeDemo" class="tree"></ul>
		</div>		
		</TD>
		
	</TR>
</TABLE>
 
 </BODY>
</HTML>